package com.onland.dfish.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.onland.dfish.entity.Comments;
import com.onland.dfish.entity.Users;
import com.onland.dfish.service.ICommentsService;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@Controller
@RequestMapping("comments")
public class CommentsController {
    @Autowired
    private ICommentsService commentsService;

    /**
     * 添加评论
     * @param comments
     * @return
     */
    @PostMapping("add")
    public String addComments(Comments comments, HttpSession session){
        Users users=(Users) session.getAttribute("users");
        if(users!=null){
            int userId=users.getId();
            comments.setUserId(userId);
        }
        commentsService.insert(comments);
        return "redirect:/video/play_video.jsp";
    }

    /**
     * 根据视频id查询评论
     * @param videoId
     * @param request
     * @return
     */
    @PostMapping("query")
    public String queryComments(int videoId, HttpServletRequest request){
        List<Comments> commentsList=commentsService.queryComments(videoId);
        request.setAttribute("commentsList",commentsList);
        return "/video/play_video.jsp";

    }
}
